# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include "NArrayTree.c"
# include "MountVFS.c"
# include "Hashing.c"
# include "BinarySearchTree.c"

/*struct FileDescriptor {
	char fileName[50];
	char filePath[100];
	char fileType[5];
	//int size;
	//Location Block Number
};*/

/*typedef struct VFS {
	char label[50];
	int freeList[30];
	struct FileDescriptor *fd[30];
	char block[30][1024];
}vfs;*/

struct VFS *create_block (char *label) {
	struct VFS * temp;
	int i;
	FILE *fp;
	temp = (struct VFS *) malloc (sizeof(struct VFS));
	strcpy (temp -> label, label);
	/*for (i = 0; i < 30; i++) {
		temp -> freeList[i] = 0;
		temp -> fd[i] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
		temp -> fd[i] -> fileName[0] = '\0';
		temp -> fd[i] -> filePath[0] = '\0';
		temp -> fd[i] -> fileType[0] = '\0';
	}*/
	return temp;
}

void splitFileNames (struct VFS *header) {
	int i, j, k = 0, l;
	char tempPath[100],tempFN[50],tempEx[5];
	for (i = 0; i < MAX_FD_SIZE; i++) {
		for (l = 0; l < 50; l++)
			tempFN[l] = '\0';
		for (l = 0; l < 5; l++)
			tempEx[l] = '\0';
		if(header -> freeList[i]) {
			for (j = 0; header -> fd[i].filePath[j]; j++) {
				if ( header -> fd[i].filePath[j] == '/') {
					for (l = 0; l < 50 && tempFN[l]; l++)
						tempFN[l] = '\0';
					k = 0;
				}
				else {
					tempFN[k++] = header -> fd[i].filePath[j];
					//tempFN[k++] = tempPath[j];
				}
			}
			tempFN[j] = '\0';
			j = 0, k = 0;
			while (tempFN[j++] != '.');
			while (tempFN[j + k]) {
				tempEx[k] = tempFN[j + k];
				k++;
			}
			j--;
			while (tempFN[j])
				tempFN[j++] = '\0';
			tempEx[k] = '\0';
			copyString(header -> fd[i].fileName, tempFN);
			copyString(header -> fd[i].fileType, tempEx);
		}
	}
}

void read_block (struct VFS *header) {	
	char *fileName, str[100];
	FILE *fp;
	int i = 0;
/*	printf ("Enter the file name to mount: ");
	scanf ("%s", fileName);

	*/
	fp = fopen("input.txt","r");
	while (fgets(str, 100, fp)) {
		header -> freeList[i] = 1;
   		copyString (header -> fd[i++].filePath, str);
		printf("Line: %d\n", i);
	}
	fclose(fp);

	splitFileNames(header);
}

int main () {
	struct VFS *mainHeader;
	create_block("MyVFS");
	read_block(mainHeader);
	mount_vfs(mainHeader);
}
